/**
 * Copyright (c) 2012-2016, www.tinygroup.org (luo_guo@icloud.com).
 * <p>
 * Licensed under the GPL, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.tinygroup.databasebuinstaller.impl;

import org.tinygroup.database.initdata.InitDataProcessor;
import org.tinygroup.database.util.DataBaseUtil;
import org.tinygroup.database.util.MetadataInfos;
import org.tinygroup.logger.LogLevel;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * 功能说明:数据库初始化安装处理器 

 * 开发人员: renhui <br>
 * 开发时间: 2013-8-15 <br>
 * <br>
 */
public class InitDataInstallProcessor extends AbstractInstallProcessor {
    private static final String INIT_DATA_KEY = "INIT_DATA";

    private InitDataProcessor initDataProcessor;


    public InitDataProcessor getInitDataProcessor() {
        return initDataProcessor;
    }


    public void setInitDataProcessor(InitDataProcessor initDataProcessor) {
        this.initDataProcessor = initDataProcessor;
    }

    public int getOrder() {
        return LOWEST_PRECEDENCE;
    }


	@Override
	public List<String> getProcessSqls(boolean isFull, String language,
			Connection connection) throws SQLException {
		logger.logMessage(LogLevel.INFO, "开始获取数据库初始化数据安装操作执行语句");
        int status = 0;
        if(MetadataInfos.getInfos()!=null && MetadataInfos.getInfos().containsKey(INIT_DATA_KEY)){
            status = MetadataInfos.getInfos().get(INIT_DATA_KEY);
        }
        List<String> sqls = new ArrayList<String>();
        if(status==1){
            logger.log(LogLevel.WARN,"该库已经被初始化,若要强制初始化,请修改状态!");
            return sqls;
        }
        sqls.addAll(initDataProcessor.getDeInitSql(language));
        sqls.addAll(initDataProcessor.getInitSql(language));
        if (sqls.size() != 0) {
            logger.logMessage(LogLevel.INFO, "生成sql:{0}", sqls);
            String from = DataBaseUtil.fromSourceLocal.get();
            //非工具
            if(!"tool".equals(from)){
                Statement statement = connection.createStatement();
                statement.execute(
                        "UPDATE TINY_METADATA_INFO SET  META_DATA_STATUS=1 WHERE META_DATA_TYPE = 'INIT_DATA'");
                MetadataInfos.resetInfo(INIT_DATA_KEY,1);
                logger.logMessage(LogLevel.WARN,"初始化语句已生成,已将init_data状态改变。");
            }
        } else {
            logger.logMessage(LogLevel.INFO, "无需生成Sql");
        }
        logger.logMessage(LogLevel.INFO, "获取数据库初始化数据安装操作执行语句结束");
        return sqls;
	}
    
    
}
